#include <iostream>
#include <vector>
#include <rose.h>
#include "printer.hpp"
#include "translator.hpp"

using std::cout;
using std::endl;
using std::vector;
using std::string;

using namespace SageBuilder;
using namespace SageInterface;


int fortran_to_cpp(vector<string> argv);

int main(int _argc, char* _argv[]) {
    #if DEBUG
        cout << ">> ftc::main " << _argc << (vector<string>(_argv,_argv+_argc)) << endl;
    #endif
   
    vector<string> argv (_argv,_argv+_argc);
    if(argv.empty()) {
        cout << "nothing to be done." << endl;
        return 0;
    }
    
    int errc = fortran_to_cpp(argv);
    if(errc!=0) return errc;
    
    return 0;
}

int fortran_to_cpp(vector<string> argv) {
    #if DEBUG
        cout << ">> fortran_to_cpp " << argv << endl;
    #endif

    SgProject* proj = frontend(argv);
    if(proj==NULL) {
        cout << "Cat. Error: Couldn't create SgProject" << endl;
        return 1;
    }
    
    SgProject* nproj = NULL;
    
    for(int i = 0; i<proj->numberOfFiles(); i++) {
        SgFile& file = proj->get_file(i);
        #if DEBUG
            cout << ".. process \"" << file.getFileName() << "\"" << endl;
            AstPrinter printer;
            printer.traverse(&file);
        #endif
        
        SgFile* nfile = ftc::xf_file(&file, &nproj);
        
        #if DEBUG
            cout << ".. translated \"" << nfile->get_unparse_output_filename() << "\"" << endl;
            printer.traverse(nfile);
        #endif
    }
    
    if(nproj!=NULL)
        nproj->unparse();

    return 0;
}


